import { Controller, Get, Post, UploadedFiles, UseInterceptors, Req } from '@nestjs/common';
import { IndexService } from './index.service';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { Mold } from '../entity/mold.entity';
@Controller("mold")
export class IndexController {
  constructor(private readonly moldService: IndexService) {}

  @Get("/list")
  async getMoldList(): Promise<any[]> {
    const list = await this.moldService.findAllMold();
    return list;
  }

  @Post("/upload")
  @UseInterceptors(AnyFilesInterceptor())
  async upload(@UploadedFiles() files: Express.Multer.File[], @Req() req) {
    // files 是所有上传的文件
    // moldId 在 req.body 里
    // 例如：req.body['files[0][moldId]']、req.body['files[1][moldId]']
    let arr: any[] = req.body.files;
    const result = files.map((file, idx) => ({
      moldId: arr[idx].moldId,
      file: file,
      tenantId: arr[idx].tenantId,
    }));
    const res = await this.moldService.uploadFiles(result);
    return res;
  }
}
